﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
using System.Diagnostics;

namespace freedisk
{
    class DiskInfo
    {

        public enum DriveFormats
        {
            fat12, fat16, fat32, hpfs, ntfs
        }

        public enum DriveTypes:int
        {
            Unknown = 0,
            NoRootDirectory = 1,
            RemoveableDisk = 2,
            LocalDisk = 3,
            NetworkDrive = 4,
            CompactDisk = 5,
            RAMDisk = 6
        }


        string m_driveLetter;
        UInt64 m_driveTotalBytes;
        UInt64 m_driveFreeBytes;
        DriveTypes m_driveType;
        string m_machineName;


        private void Fill(string driveLetter, string machineToCheck)
        {

            if (String.IsNullOrEmpty(driveLetter) || String.IsNullOrEmpty(machineToCheck))
            {
                throw new ArgumentException("driveLetter and machineToCheck must not be null or empty");
            }

            string selectString = "SELECT * FROM Win32_LogicalDisk WHERE Name = '" + driveLetter + "'";
            ManagementScope scope = new ManagementScope("\\\\" + machineToCheck + "\\root\\cimv2");
            SelectQuery select = new SelectQuery(selectString);
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, select))
            {

                foreach (ManagementObject disk in searcher.Get())
                {
                    m_driveTotalBytes = Convert.ToUInt64(disk.GetPropertyValue("Size"));
                    m_driveFreeBytes = Convert.ToUInt64(disk.GetPropertyValue("FreeSpace"));
                    m_driveType = (DriveTypes) Convert.ToInt32(disk.GetPropertyValue("DriveType"));

                }
            }

        }

        public DiskInfo()
        {
            /* This is bad, because we cant assume that there is always a C:
             * (diskless workstations, etc)
             * (Work Item 1056)
             */
            
            m_machineName = ".";
            m_driveLetter = "c:";

            this.Fill(m_driveLetter, m_machineName);
        }

        public DiskInfo(string driveLetter)
        {
            m_machineName = ".";
            m_driveLetter = driveLetter;
            this.Fill(m_driveLetter, m_machineName);
        }

        public DiskInfo(string driveLetter, string machineName)
        {

            m_machineName = machineName;
            m_driveLetter = driveLetter;
            this.Fill(m_driveLetter, m_machineName);

        }

        public DriveTypes DriveType
        {
            get { return m_driveType; }
        }

        public string DriveLetter
        {
            get { return m_driveLetter; }
        }

        public string Machine
        {
            get { return m_machineName; }
        }




    }
}
